home *** CD-ROM | disk | FTP | other *** search
- unit piegraf;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs;
-
- type
-
- TPieGraphic = class;
-
- TPieWedge = class(TCollectionItem)
- private
- FWedgeValue: Integer; // Value which this wedge represents
- FColor: TColor; // Color to paint the wedge
- FBrush: TBrush; // Brush object to use in painting the wedge
- public
- constructor Create(Collection: TCollection); override;
- destructor Destroy; override;
- procedure Assign(Source: TPersistent); override;
- procedure SetWedgeValue(Value: Integer);
- procedure SetColor(Value: TColor);
- published
- { The published properties will be streamed automatically. }
- property WedgeValue: Integer read FWedgeValue write SetWedgeValue;
- property Color: TColor read FColor write SetColor;
- end;
-
- TPiePieces = class(TCollection)
- private
- FPieGraphic: TPieGraphic; // Owner component of this property
- FTotal: Integer; // Total of all TPieWedge WedgeValues
- function GetItem(Index: Integer): TPieWedge;
- procedure SetItem(Index: Integer; Value: TPieWedge);
- protected
- procedure Update(Item: TCollectionItem); override;
- public
- constructor Create(PieGraphic: TPieGraphic);
- function Add: TPieWedge;
- procedure UpdatePiePieces;
- function AddPiece(Value: Integer; wColor: TColor): TPieWedge;
- property Items[Index: Integer]: TPieWedge read GetItem
- write SetItem; default;
- property Total: Integer read FTotal;
- end;
-
- TPieGraphic = class(TGraphicControl)
- private
- FPiePieces: TPiePieces; // Private storage for the TPiePieces property
- protected
- procedure SetPiePieces(Value: TPiePieces);
- public
- procedure Paint; override;
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure AddPiece(Value: Integer; wColor: TColor);
- published
- property PiePieces: TPiePieces read FPiePieces write SetPiePieces;
- end;
-
- procedure Register;
-
- implementation
- uses DsgnIntf, Piegrpe;
-
- { TPieWedge }
-
- constructor TPieWedge.Create(Collection: TCollection);
- { Calls inherited Create and passes collection which is then linked to this
- TPieWedge instance. Also creates the FBrush instance. }
- begin
- inherited Create(Collection);
- FBrush := TBrush.Create;
- end;
-
- destructor TPieWedge.Destroy;
- { Frees the FBrush instance before calling the inherited Destroy destructor. }
- begin
- FBrush.Free;
- inherited Destroy;
- end;
-
- procedure TPieWedge.Assign(Source: TPersistent);
- { Copies the data from the TPieWedge parameter to this instances properties. }
- begin
- if Source is TPieWedge then begin
- { Assign property values via the property instead of directly to the
- private storage so that the side-effects occur }
- WedgeValue := TPieWedge(Source).WedgeValue;
- Color := TPieWedge(Source).Color;
- Exit;
- end;
- inherited Assign(Source);
- end;
-
- procedure TPieWedge.SetWedgeValue(Value: Integer);
- { Copies data to the private storage. Also invokes Changed which causes the
- linked TCollection to call it's Update method. See TPiePieces.Update. }
- begin
- FWedgeValue := Value;
- Changed(False);
- end;
-
- procedure TPieWedge.SetColor(Value: TColor);
- { Copies data to the private storage and sets FBrush.Color as well. Also
- invokes Changed which causes the linked TCollection to call it's
- Update method. See TPiePieces.Update. }
- begin
- FColor := Value;
- FBrush.Color := Value;
- Changed(False);
- end;
-
- { TPiePieces }
-
- constructor TPiePieces.Create(PieGraphic: TPieGraphic);
- { Calls the inherited Create constructor, passing the TCollectionItem type
- so that this collection knows the type with which it will be used. Also
- points it's private field FPieGraphic to the PieGraphic instance. }
- begin
- inherited Create(TPieWedge);
- FPieGraphic := PieGraphic;
- end;
-
- function TPiePieces.GetItem(Index: Integer): TPieWedge;
- { Returns a reference to the Index'th TPieWedge in the collection }
- begin
- Result := TPieWedge(inherited GetItem(Index));
- end;
-
- procedure TPiePieces.SetItem(Index: Integer; Value: TPieWedge);
- { Sets the Index'th TPieWedge instance with the specified values. }
- begin
- inherited SetItem(Index, Value);
- end;
-
- function TPiePieces.Add: TPieWedge;
- { A wrapper around the TCollection.Add method. This Add method returns a
- reference of the TPieWedge type whereas TCollectionAdd returns a reference
- to a TCollectionItem type. This method just prevents the user of this
- colletion from having to perform this step. }
- begin
- Result := TPieWedge(inherited Add);
- end;
-
- function TPiePieces.AddPiece(Value: Integer; wColor: TColor): TPieWedge;
- { Adds a new TPieWedge to the collection with the specified value and color. }
- begin
- Result := Add;
- Result.WedgeValue := Value;
- Result.Color := wColor;
- end;
-
- procedure TPiePieces.UpdatePiePieces;
- { Repaints FPieGraphic }
- begin
- FPieGraphic.Refresh;
- end;
-
- procedure TPiePieces.Update(Item: TCollectionItem);
- { TCollection.Update is called by TCollectionItems
- whenever a change is made to any of the collection items. This is
- initially an abstract method. It must be overriden to contain
- whatever logic is necessary when a TCollectionItem has changed. }
- var
- i: integer;
- begin
- { Since the user may have added or removed an item, recalculate the
- FTotal value. }
- FTotal := 0;
- for i := 0 to Count - 1 do
- FTotal := FTotal + Items[i].WedgeValue;
- if Item <> nil then
- UpdatePiePieces;
- end;
-
- { TPieGraphic }
-
- constructor TPieGraphic.Create(AOwner: TComponent);
- { Creates a TPiePieces instance and sets a defalt component size. }
- begin
- inherited Create(AOwner);
- FPiePieces := TPiePieces.Create(self);
- Width := 200;
- Height := 200;
- end;
-
- destructor TPieGraphic.Destroy;
- { Frees the TPiePieces instance. }
- begin
- FPiePieces.Free;
- inherited Destroy;
- end;
-
- procedure TPieGraphic.AddPiece(Value: Integer; wColor: TColor);
- { An interface function that adds a new pie wedge to the collection }
- begin
- FPiePieces.AddPiece(Value, wColor);
- Refresh;
- end;
-
- procedure TPieGraphic.SetPiePieces(Value: TPiePieces);
- { Write access method for the PiePieces property }
- begin
- FPiePieces.Assign(Value);
- end;
-
- procedure TPieGraphic.Paint;
- { Iterates through each TPieWedge in FPiePieces' collection and paints it
- using the specified color. }
- var
- StartA, EndA: Integer;
- midX, midY, stX, stY, endX, endY: Integer;
- sX, sY, eX, eY: double;
- i: integer;
- begin
-
- if FPiePieces.FTotal <> 0 then begin
- StartA := 0;
- for i := 0 to FPiePieces.Count - 1 do begin
- if i = FPiePieces.Count - 1 then
- EndA := 0
- else begin
- EndA := StartA + Trunc((Integer(FPiePieces.Items[i].FWedgeValue) /
- FPiePieces.FTotal) * 360);
- if EndA = StartA then EndA := StartA+1;
- end;
-
- midX := Width div 2;
- midY := Height div 2;
-
- sX := Cos((StartA / 180.0) * pi);
- sY := Sin((StartA / 180.0) * pi);
- eX := Cos((EndA / 180.0) * pi);
- eY := Sin((EndA / 180.0) * pi);
-
- stX := Round(sX * 100);
- stY := Round(sY * 100);
- endX := Round(eX * 100);
- endY := Round(eY * 100);
-
- with Canvas do
- begin
- { Copy the brush from the TPieWedge to this Canvas }
- Brush := FPiePieces.Items[i].FBrush;
- Pie(0,0, Width,Height, midX + stX, midY - stY,
- midX + endX, midY - endY);
- end;
- StartA := EndA;
- end;
- end;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Test', [TPieGraphic]);
- RegisterPropertyEditor(TypeInfo(TPiePieces), TPieGraphic,
- 'PiePieces', TPiePiecesProperty);
- end;
-
- end.
-